const CompressionWebpackPlugin = require('compression-webpack-plugin');
const FileManagerPlugin = require('filemanager-webpack-plugin');
const isProduction = process.env.NODE_ENV === 'production';

let plugins = [];
let entryApp = [];
let model = process.env.VUE_APP_MODEL;
if (model === 'mock' && !isProduction) {
    //npm run mock 时引入
    entryApp.push('./src/mock/index.js');
}

if(isProduction){
    plugins.push(
        new CompressionWebpackPlugin({
        algorithm: 'gzip',
        test: /\.js$|\.html$|\.css/,
        threshold: 10240, // 只有大小大于该值的资源会被处理 10240
        minRatio: 0.8, // 只有压缩率小于这个值的资源才会被处理
        deleteOriginalAssets: false // 删除原文件
        })
    );
    plugins.push(
        new FileManagerPlugin({
            events: {
                onEnd: {
                    copy: [
                        { source: './dist/app/index.html', destination: './src/index.vm' },
                        //{ source: '/path/**/*.js', destination: '/path' },
                    ],
                }
            }
        })
    )
}

module.exports = {
    outputDir: `dist${process.env.VUE_APP_PUBLICPATH}`,
    publicPath: process.env.VUE_APP_PUBLICPATH,
    productionSourceMap:false,
    configureWebpack: {
        mode:'production',
        entry: {
            app: entryApp
        },
        externals: {
            //'vue': 'Vue',
            //'vue-router': 'VueRouter',
            //'axios': 'axios'
        },
        optimization: {
            splitChunks: {
                chunks: "async",
                name: false,
                minSize: 51200,  //大于50k
                // minChunks: 5,  //被5模块共享
                maxAsyncRequests: 5,  //所有异步请求不得超过5个
                maxInitialRequests: 3,  //初始话并行请求不得超过3个
                cacheGroups:{
                    vantcss: {
                        test: /[\\/]node_modules[\\/](vant)[\\/].*(.css|.less)$/,
                        name: 'vantcss.vendors',
                        chunks: 'async'
                    }
                }
            }
        },
        plugins:plugins
    },
    chainWebpack:config =>{
        // 移除 prefetch 插件
        config.plugins.delete("prefetch");
        // 移除 preload 插件
        config.plugins.delete('preload');
    },
    css: {
        loaderOptions: {
            sass: {// 配置scss 全局样式文件 支持全局变量
                additionalData: `@import "~@/assets/scss/base/common.scss";`
            },
        }
    },
};